/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelGraficas.java
 *
 * Created on 27/10/2010, 09:51:23 PM
 */
package interfaz;

import funcion.ManejadorFuncion;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.SwingConstants;
import org.jdesktop.swingx.JXGraph;
import org.jdesktop.swingx.JXGraph.Plot;
import org.jdesktop.swingx.JXHyperlink;

/**
 *
 * @author The NuMethod Team
 */
public class PanelGraficas extends javax.swing.JPanel {

    NuMethodApp parent;
    String defaultText = "Inserta tu funcion";
    String placedText = "";

    /** Creates new form PanelGraficas */
    public PanelGraficas(NuMethodApp parent) {
        this.parent = parent;
        initComponents();
        initComponents2();
    }

    public void setLinksColor (Color c){
        LinkGoToNoLineales.setClickedColor(c);
        LinkGoToNoLineales.setUnclickedColor(c);
        jXHyperlink1.setUnclickedColor(c);
        jXHyperlink1.setClickedColor(c);
    }
    public JXHyperlink getLinkGoToNoLineales() {
        return LinkGoToNoLineales;
    }

    private class plotie extends Plot {

        @Override
        public double compute(double d) {
            return 3 * (Math.pow(d, 2));
        }
    }

    private class NuMethodPlot extends Plot {

        private String funcion;

        public NuMethodPlot(String funcion) {
            super();
            this.funcion = funcion;
        }

        @Override
        public double compute(double d) throws NullPointerException {
            double te = ManejadorFuncion.getInstance().eval("f", d);
            return te;
        }
    }

    private void initComponents2() {

        plotie myPlotie = new plotie();


        Point2D origin = new Point2D.Double(0.0d, 0.0d);
        Rectangle2D view = new Rectangle2D.Double(-10.0d, -10.0d, 20.0d, 20.0d);

        // GraphFuncion = new JXGraph(origin, view);
        GraphFuncion.setOrigin(origin);
        GraphFuncion.setView(view);

        GraphFuncion.setMajorX(2);
        GraphFuncion.setMajorY(2);
        GraphFuncion.setMinorCountX(2);
        GraphFuncion.setMinorCountY(2);
        //GraphFuncion.setView(new Rectangle2D.Double(10, 10, 200, 200));


//        GraphFuncion.setMajorX(0.5);
//        GraphFuncion.setMajorY(0.5);
        GraphFuncion.setAxisPainted(true);
        GraphFuncion.setTextPainted(true);
        GraphFuncion.setInputEnabled(true);
        GraphFuncion.setGridPainted(true);
        GraphFuncion.addPlots(Color.yellow, myPlotie);

        FieldGraphableFunction.setText(defaultText);
        FieldGraphableFunction.setForeground(Color.lightGray);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        GraphFuncion = new org.jdesktop.swingx.JXGraph();
        FieldGraphableFunction = new javax.swing.JTextField();
        LinkGoToNoLineales = new org.jdesktop.swingx.JXHyperlink();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();

        setToolTipText("Graficador");

        GraphFuncion.setAlpha(0.9F);
        GraphFuncion.setPreferredSize(new java.awt.Dimension(500, 500));

        javax.swing.GroupLayout GraphFuncionLayout = new javax.swing.GroupLayout(GraphFuncion);
        GraphFuncion.setLayout(GraphFuncionLayout);
        GraphFuncionLayout.setHorizontalGroup(
            GraphFuncionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 500, Short.MAX_VALUE)
        );
        GraphFuncionLayout.setVerticalGroup(
            GraphFuncionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 514, Short.MAX_VALUE)
        );

        FieldGraphableFunction.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                FieldGraphableFunctionMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                FieldGraphableFunctionMouseExited(evt);
            }
        });
        FieldGraphableFunction.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                FieldGraphableFunctionFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                FieldGraphableFunctionFocusLost(evt);
            }
        });

        LinkGoToNoLineales.setText("Ir a Ecuaciones no Lineales");
        LinkGoToNoLineales.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LinkGoToNoLinealesActionPerformed(evt);
            }
        });

        jButton1.setText("Analizar y Graficar");
        jButton1.setBorderPainted(false);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton3.setText("Añadir a disponibles");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton2.setText("Remover Plots");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jXHyperlink1.setText("Ir a Inicio");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addComponent(GraphFuncion, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(FieldGraphableFunction, javax.swing.GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jButton2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1))
                    .addComponent(jButton3)
                    .addComponent(LinkGoToNoLineales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(FieldGraphableFunction, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 364, Short.MAX_VALUE)
                        .addComponent(jButton3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(LinkGoToNoLineales, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(9, 9, 9))
                    .addComponent(GraphFuncion, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE))
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents

    // <editor-fold defaultstate="collapsed" desc="Metodos para generar el efecto de Prompt Text en el field">
    private void showFieldPrompt() {
        if (FieldGraphableFunction.getText().equalsIgnoreCase(defaultText) || FieldGraphableFunction.getText().equalsIgnoreCase("")) {
            FieldGraphableFunction.setText("");
            FieldGraphableFunction.setForeground(Color.black);
        }

    }

    private void hideFieldPrompt() {
        if (FieldGraphableFunction.getText().equalsIgnoreCase("")) {
            FieldGraphableFunction.setText(defaultText);
            FieldGraphableFunction.setForeground(Color.lightGray);
        }
    }// </editor-fold>

    private void FieldGraphableFunctionFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_FieldGraphableFunctionFocusGained
    }//GEN-LAST:event_FieldGraphableFunctionFocusGained

    private void FieldGraphableFunctionFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_FieldGraphableFunctionFocusLost
    }//GEN-LAST:event_FieldGraphableFunctionFocusLost

    private void FieldGraphableFunctionMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FieldGraphableFunctionMouseEntered
        showFieldPrompt();
    }//GEN-LAST:event_FieldGraphableFunctionMouseEntered

    private void FieldGraphableFunctionMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_FieldGraphableFunctionMouseExited
        hideFieldPrompt();
    }//GEN-LAST:event_FieldGraphableFunctionMouseExited

    private void LinkGoToNoLinealesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LinkGoToNoLinealesActionPerformed
        parent.getPanelMain().cardChanger(PanelMain.ECUACIONES);
        parent.panelChanger(NuMethodApp.MAIN);
    }//GEN-LAST:event_LinkGoToNoLinealesActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            if (!FieldGraphableFunction.getText().equals(defaultText)
                    || !FieldGraphableFunction.getText().equals("")) {
                String function = FieldGraphableFunction.getText();
                ManejadorFuncion.getInstance().addFuncion("f", "x", function);


                NuMethodPlot nmPlot = new NuMethodPlot(FieldGraphableFunction.getText());
                GraphFuncion.addPlots(Color.black, nmPlot);
                GraphFuncion.repaint();

                if (ManejadorFuncion.getInstance().eval("f", 0) != Double.NaN) //Verificar que si se puede parsear la expresion
                {
                    jLabel1.setForeground(Color.green);
                    jLabel1.setText("Ecuacion parseada correctamente");
                } else {
                    jLabel1.setForeground(Color.red);
                    jLabel1.setText("Error parseando la expresion");

                }
            }
        } catch (Exception npe) {
            jLabel1.setForeground(Color.red);
            jLabel1.setText("Error parseando la expresion, por favor analizarla");
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        parent.getPanelMain().getPanelEcuaciones().addToAvailables(FieldGraphableFunction.getText());
        jLabel1.setText("Ecuacion añadida");
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        GraphFuncion.removeAllPlots();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        parent.panelChanger(NuMethodApp.WELCOME);
    }//GEN-LAST:event_jXHyperlink1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField FieldGraphableFunction;
    private org.jdesktop.swingx.JXGraph GraphFuncion;
    private org.jdesktop.swingx.JXHyperlink LinkGoToNoLineales;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    // End of variables declaration//GEN-END:variables

    public static void main(String[] args) {
        JFrame m = new JFrame();
        m.setTitle("2");
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PanelGraficas p = new PanelGraficas(null);

        m.add(p);
        m.pack();

        m.setVisible(true);
    }
}
